package lc_1016_子串能表示从1到N数字的二进制串;

import java.util.HashSet;
import java.util.Set;

/**
 * 1. 直接申请10^9内存，立刻超内存
 * 2. hashset不是因为能超10**9，是因为字符串长度1000的情况下，根本不能达到10**9个数量
 */
public class Solution {

	public boolean queryString1(String s, int n) {
		for (int i = 1; i <= n; i++) {
			if (!s.contains(Integer.toBinaryString(i)))
				return false;
		}
		return true;
	}

	/**
	 * 超内存
	 * @param s
	 * @param n
	 * @return
	 */
	public boolean queryString2(String s, int n) {
		boolean[] b = new boolean[n + 1];
		b[0] = true;
		int cnt = 0;
		for (int i = 0; i < s.length(); i++) {
			int num = 0;
			for (int j = 0; j < 32 && i - j >= 0 && (num |= (s.charAt(i-j) - '0') << j) <= n; j++) {
				if (!b[num]) {
					b[num] = true;
					cnt++;
				}
			}
		}
		return cnt == n;
	}
	
	/**
	 * 未超内存
	 * @param s
	 * @param n
	 * @return
	 */
	public boolean queryString(String s, int n) {
		Set<Integer> b = new HashSet<>();
		b.add(0);
		int cnt = 0;
		for (int i = 0; i < s.length(); i++) {
			int num = 0;
			for (int j = 0; j < 32 && i - j >= 0 && (num |= (s.charAt(i - j) - '0') << j) <= n; j++) {
				if (b.add(num)) {
					cnt++;
				}
			}
		}
		return cnt == n;
	}
	

	public boolean queryString3(String s, int n) {
		boolean[] b = new boolean[n + 1];
		char[] cs = s.toCharArray();
		b[0] = true;
		int cnt = 0;
		for (int i = 0; i < s.length(); i++) {
			int num = 0;
			for (int j = 0; j < 32 && i - j >= 0 && (num |= (cs[i - j] - '0') << j) <= n; j++) {
				num |= (cs[i - j] - '0') << j;
				if (num > n) {
					break;
				}
				if (!b[num]) {
					b[num] = true;
					cnt++;
				}
			}
		}
		return cnt == n;
	}

	public boolean queryString4(String s, int n) {
		int len = s.length();
		if (n > len * len) {
			return false;
		}
		char[] cs = s.toCharArray();
		int[] nums = new int[len];
		for (int i = 0; i < len; i++) {
			nums[i] = cs[i] - '0';
		}
		boolean[] b = new boolean[n + 1];
		b[0] = true;
		int size = Integer.toBinaryString(n).length();
		int cnt = 0;
		for (int i = 1; i <= size; i++) {
			int window = (1 << i) - 1;
			int num = 0, p = 0;
			while (p < len) {
				num <<= 1;
				if (nums[p] == 1) {
					num |= 1;
				}
				num &= window;
				if (num <= n && !b[num]) {
					cnt++;
					b[num] = true;
				}
				p++;
			}
		}
		return cnt == n;
	}


	
	
//	"011010101010111101010101011111111111111111111111111111111110000000000000011111101010101001010101010101010101010101111010101010111111111111111111111111111111111100000000000000111111010101010010101010101010101010100"
//	1000000000
}
